package cn.mdmbct.douyin.spring.example.controller;

import cn.mdmbct.douyin.basis.body.video.VideoCreateReqBody;
import cn.mdmbct.douyin.basis.response.video.VideoCreateRes;
import cn.mdmbct.douyin.basis.response.video.VideoPartInitRes;
import cn.mdmbct.douyin.basis.response.video.VideoUploadRes;
import cn.mdmbct.douyin.basis.service.interfaces.DyBasisService;
import cn.mdmbct.douyin.common.enums.ApiPlatform;
import cn.mdmbct.douyin.spring.example.dto.DyVideoPublishDto;
import cn.mdmbct.douyin.spring.example.service.DyVideoPushService;
import cn.mdmbct.douyin.spring.example.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 抖音视频接口
 *
 * @author mdmbct  mdmbct@outlook.com
 * @date 2021/3/12 9:32
 * @modified mdmbct
 * @since 1.0
 */
@RestController
@RequestMapping("/dy/video")
@Api(tags = "抖音视频接口")
public class DyVideoController {


    @Resource
    private DyBasisService dyBasisService;

    @Resource
    private DyVideoPushService videoPushService;

    @ApiOperation("视频上传")
    @GetMapping("/upload")
    public Result<VideoUploadRes.VideoUploadResponseVideo> upload(String openId, String filePath) throws FileNotFoundException {

        File videoFile = new File(filePath);

        VideoUploadRes.VideoUploadResponseData videoUploadResponseData = dyBasisService.getVideoService().autoUploadVideo(openId, videoFile, ApiPlatform.DOU_YIN);

        return Result.success(videoUploadResponseData.getVideo());

    }


    @ApiOperation("视频分片初始化")
    @GetMapping("/upload/part/init")
    public Result<VideoPartInitRes.VideoPartInitResData> partInit(String openId) {

        VideoPartInitRes.VideoPartInitResData videoPartInitResData = dyBasisService.getVideoService().videoPartInit(openId, ApiPlatform.DOU_YIN);
        return Result.success(videoPartInitResData);
    }


    @ApiOperation("创建视频")
    @GetMapping("/create")
    public Result<VideoCreateRes.VideoCreateResData> create(String openId, float cover, String videoId) {

        VideoCreateReqBody build = VideoCreateReqBody.builder()
                .coverTsp(cover)
                .videoId(videoId)
                .build();

        VideoCreateRes.VideoCreateResData video = dyBasisService.getVideoService().videoCreate(openId, build, ApiPlatform.DOU_YIN);
        return Result.success(video);

    }

    @ApiOperation("发布视频 返回发布的抖音视频id")
    @PostMapping("/publish")
    public Result<String> publishVideo(@RequestBody DyVideoPublishDto dto) {

        videoPushService.pushVideo(dto);
        return Result.success();
    }
}